package com.longge;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @program: my-code
 * @description:
 * @author: longge
 * @create: 2021-04-21 01:01
 */
public class RotateRight {

    public ListNode rotateRight(ListNode head, int k) {
        if (head == null) return head;
        Deque<Integer> deque = new LinkedList<>();
        ListNode node = new ListNode(0), cur = node;
        while (head != null) {
            deque.offer(head.val);
            head = head.next;
        }
        int size = deque.size();
        k = size % k;
        while (k != 0) {
            k--;
            Integer pollLast = deque.pollLast();
            deque.offerFirst(pollLast);
        }
        for (Integer val : deque) {
            cur.next = new ListNode(val);
            cur = cur.next;
        }
        return node.next;
    }
}
